
import cloud from '@lafjs/cloud'
import nw from "nw-lafjs";
import {fail, ok} from "@/utils/responseUtil";
const  db = cloud.database()
const _ = db.command
/**
 * 删除以前的数据
 * @param body
 */
async function deleteOldSkuInfo(body: any) {
  return nw.db.del({
    dbName:"skuInfo",
    whereJson:{_id:_.eq(body._id)}
  })
}

async function deleteOldSkuPostes(body: any) {
  return nw.db.update({
    dbName:"skuPoster",
    whereJson:{skuId:_.eq(body._id)},
    dataJson:{deleted:1}
  })
}

async function deleteOldSkuImage(body: any) {
  return nw.db.update({
    dbName:"skuImage",
    whereJson:{skuId:_.eq(body._id)},
    dataJson:{deleted:1}
  })
}

async function deleteOldSkuAttrValue(body: any) {
  return nw.db.update({
    dbName:"skuAttrValue",
    whereJson:{skuId:_.eq(body._id)},
    dataJson:{deleted:1}
  })
}

async function saveSkuInfo(body: any) {
  //构建返回结果
  let data = {
    _id:body._id,
    categoryId:body.categoryId,
    attrGroupId:body.attrGroupId,
    skuType:body.skuType,
    skuName:body.skuName,
    perLimit:body.perLimit,
    isNewPerson:body.isNewPerson,
    sort:body.sort,
    imgUrl:body.imgUrl,
    skuCode:body.skuCode,
    price:body.price,
    marketPrice:body.marketPrice,
    stock:body.stock,
    lowStock:body.lowStock,
    createTime:new Date(),
    updateTime:new Date(),
    deleted:0
  }
  //1.直接保存即可
  return cloud.mongo.db.collection("skuInfo").insertOne(data)
}

async function saveSkuPoster(body: any) {
  let skuPosterList = body.skuPosterList
  let skuPosters = []
  for (let i = 0; i < skuPosterList.length; i++) {
    let each = skuPosterList[i]
    let post = {
      skuId:body.skuId,
      imgName:each.imgName,
      imgUrl:each.imgUrl,
      sort:each.sort,
      createTime:new Date(),
      updateTime:new Date(),
      deleted:0
    }
    skuPosters.push(post)
  }
  return  db.collection("skuPoster").add(skuPosters,{multi:true})

}

async function saveSkuImages(body: any) {
  let skuOmagesList = body.skuImagesList
  let skuImages = []
  for (let i = 0; i < skuOmagesList.length; i++) {
    let each = skuOmagesList[i]
    let post = {
      skuId:body.skuId,
      imgName:each.imgName,
      imgUrl:each.imgUrl,
      sort:each.sort,
      createTime:new Date(),
      updateTime:new Date(),
      deleted:0
    }
    skuImages.push(post)
  }
  return  db.collection("skuImage").add(skuImages,{multi:true})

}

async function saveSkuAttrValue(body: any) {
  let skuAttrValueList = body.skuAttrValueList
  let skuAttrValues = []
  for (let i = 0; i < skuAttrValueList.length; i++) {
    let each = skuAttrValueList[i]
    let post = {
      skuId:body.skuId,
      attrId:each.attrId,
      attrName:each.attrName,
      attrValue:each.attrValue,
      sort:each.sort,
      createTime:new Date(),
      updateTime:new Date(),
      deleted:0
    }
    skuAttrValues.push(post)
  }
  return  db.collection("skuAttrValue").add(skuAttrValues,{multi:true})
}

export default async function (ctx: FunctionContext) {
  let body = ctx.body
  if (nw.util.isNull(body._id)){
    return fail("数据不合法")
  }
  //1.删除以前的数据
  const p1 = deleteOldSkuInfo(body)
  const p2 = deleteOldSkuImage(body)
  const p3 = deleteOldSkuPostes(body)
  const p4 = deleteOldSkuAttrValue(body)
  await Promise.all([p1,p2,p3,p4]);
  //2.构建以前的数据
  //1.保存skuInfo
  let id = await saveSkuInfo(body)
  if (nw.util.isNull(id)){
    return fail("保存失败")
  }
  body.skuId = body._id
  //2.保存skuPosterList
  if (!nw.util.isNull(body.skuPosterList)&&body.skuPosterList.length>0){
    let skuPoster = await saveSkuPoster(body)
    if (nw.util.isNull(skuPoster)||!skuPoster.ok){
      return fail("保存失败")
    }
  }

  //3.保存skuImagesList
  if (!nw.util.isNull(body.skuImagesList)&&body.skuImagesList.length>0){
    let skuImsges = await saveSkuImages(body);
    if (!skuImsges.ok){
      return fail("保存失败")
    }
  }

  //4.保存skuAttrValueList
  if (!nw.util.isNull(body.skuAttrValueList)&&body.skuAttrValueList.length>0){
    let skuAttrValue = await saveSkuAttrValue(body)
    if (!skuAttrValue.ok){
      return fail("保存失败")
    }
  }

  return ok("保存成功")


}
